package com.xiaoy.javamaildemo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import javax.annotation.PostConstruct;
import javax.mail.Session;
import java.util.Properties;

/**
 * @ClassName JavaMailConf
 * @Description TODO
 * @Author xiaoying
 * @Date 2022/6/27 11:29
 * @Version V1.0.0
 */
@Configuration
public class JavaMailConf {

    @Value("${mail.imap.protocol}")
    private String imapProtocol;
    @Value("${mail.imap.host}")
    private String imapHost;
    @Value("${mail.imap.port}")
    private int imapPort;
    @Value("${mail.smtp.protocol}")
    private String smtpProtocol;
    @Value("${mail.smtp.host}")
    private String smtpHost;
    @Value("${mail.smtp.port}")
    private int smtpPort;
    @Value("${mail.pop3.protocol}")
    private String pop3Protocol;
    @Value("${mail.pop3.host}")
    private String pop3Host;
    @Value("${mail.pop3.port}")
    private int pop3Port;
    @Value("${mail.username}")
    private String userName;
    @Value("${mail.password}")
    private String password;

    private Properties props;

    @PostConstruct
    public void init(){
        props = new Properties();
        // 表示SMTP发送邮件，需要进行身份验证
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "smtp.aliyun.com");
        props.put("mail.transport.protocol","smtp");
//        //设置端口:
        props.put("mail.smtp.port", "25");//或"25", 如果使用ssl，则去掉使用80或25端口的配置，进行如下配置：
        props.setProperty("mail.imap.usesocketchannels", "true");
        //加密方式:
        //props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        //props.put("mail.smtp.socketFactory.port", "465");
        //props.put("mail.smtp.port", "465");

//        props.put("mail.smtp.from", "1832943940@qq.com");    //mailfrom 参数
        props.put("mail.user", "ali_xiaoying@aliyun.com");// 发件人的账号(在控制台创建的发信地址)
        props.put("mail.password", "5835426Xy@");// 发信地址的smtp密码(在控制台选择发信地址进行设置)
    }
    @Bean
    public JavaMailSenderImpl javaMailSenderImpl(){
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        Properties props = new Properties();
        // 表示SMTP发送邮件，需要进行身份验证
        props.put("mail.smtp.auth", "true");
        props.setProperty("mail.imap.usesocketchannels", "true");
        javaMailSender.setJavaMailProperties(props);
        javaMailSender.setUsername(userName);
        javaMailSender.setPassword(password);
        javaMailSender.setProtocol(smtpProtocol);
        javaMailSender.setPort(smtpPort);
        javaMailSender.setHost(smtpHost);
        javaMailSender.setSession(Session.getInstance(props));
        return javaMailSender;
    }

    public String getImapProtocol() {
        return imapProtocol;
    }

    public void setImapProtocol(String imapProtocol) {
        this.imapProtocol = imapProtocol;
    }

    public String getImapHost() {
        return imapHost;
    }

    public void setImapHost(String imapHost) {
        this.imapHost = imapHost;
    }

    public int getImapPort() {
        return imapPort;
    }

    public void setImapPort(int imapPort) {
        this.imapPort = imapPort;
    }

    public String getPop3Protocol() {
        return pop3Protocol;
    }

    public void setPop3Protocol(String pop3Protocol) {
        this.pop3Protocol = pop3Protocol;
    }

    public String getPop3Host() {
        return pop3Host;
    }

    public void setPop3Host(String pop3Host) {
        this.pop3Host = pop3Host;
    }

    public int getPop3Port() {
        return pop3Port;
    }

    public void setPop3Port(int pop3Port) {
        this.pop3Port = pop3Port;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Properties getProps() {
        return props;
    }

    public void setProps(Properties props) {
        this.props = props;
    }
}
